package com.macro.mall.runner;

import cn.hutool.core.io.resource.ClassPathResource;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.api.EsIndexDto;
import com.macro.mall.remote.AdminRemote;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import java.io.InputStreamReader;

/**
 * @Author：贾维斯
 * @Description: 描述
 * @name：InitEsMappings
 * @Date：2024/10/17 16:07
 */
@Component
public class InitEsMappings  implements ApplicationRunner {
    @Autowired
    private AdminRemote adminRemote;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        //如果存在就返回
        CommonResult exists = adminRemote.exists("product_info");
        Boolean data = (Boolean) exists.getData();
        if (data) {
            return;
        }
        //文件路径
        String path ="esmapping/product_info.json";
        //糊涂工具解析文件路径
        ClassPathResource pathResource = new ClassPathResource(path);
        String io = IOUtils.toString(new InputStreamReader(pathResource.getStream(), "UTF-8"));
        EsIndexDto esIndexDto = new EsIndexDto();
        esIndexDto.setIndexMappings(io);
        esIndexDto.setIndexName("product_info");
        adminRemote.createIndex(esIndexDto);
    }
}
